package com.example.zwh.computershop.my_shopp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zwh.computershop.Myapp;
import com.example.zwh.computershop.R;
import com.example.zwh.computershop.androidUtil.ActivityCollector;
import com.example.zwh.computershop.handleDataClient.handleData;
import com.example.zwh.computershop.sqlModel.Address;

public class AddModifyAddressActivity extends AppCompatActivity implements View.OnClickListener{

    ImageView add_address_back, moidfy_adress_back;
    private TextView modify_title;

    private TextView add_new_name, add_new_phone, add_new_address;
    private Button save_addre;
    private TextView delete_address;

    private Myapp myapp;

    private int judge = 0;//判读是add还是edit
    private int state = 0;//判断是否修改成功
    private int aid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_modify_address);
        ActivityCollector.addActivity(this);
        initView();
        clidkView();
        initData();
    }

    //接收过来的数据
    public static void actionStart(Context context, String name, int aid) {
        Intent intent = new Intent(context, AddModifyAddressActivity.class);
        intent.putExtra("name", name);
        intent.putExtra("aid", aid);
        context.startActivity(intent);
    }

    void initView() {
        myapp = (Myapp) getApplication();

        add_address_back = findViewById(R.id.back_from_add_address);
        moidfy_adress_back = findViewById(R.id.back_from_modify_address);
        modify_title = findViewById(R.id.modify_title);

        add_new_name = findViewById(R.id.add_new_name);
        add_new_address = findViewById(R.id.add_new_address);
        add_new_phone = findViewById(R.id.add_new_phone);
        save_addre = findViewById(R.id.save_addre);

        delete_address = findViewById(R.id.delete_address);
    }

    void clidkView() {
        delete_address.setOnClickListener(this);
        add_address_back.setOnClickListener(this);
        moidfy_adress_back.setOnClickListener(this);
        save_addre.setOnClickListener(this);
    }
    void initData() {
        String name = getIntent().getStringExtra("name");
        aid = getIntent().getIntExtra("aid", 0);

        //修改地址
        if (name != null) {
            judge = 1;
            modify_title.setText(name);
            int len = myapp.getAddresses().size();
            for (int i = 0; i < len; i++) {
                if (myapp.getAddresses().get(i).getAddressId() == aid) {
                    add_new_name.setText(myapp.getAddresses().get(i).getName());
                    add_new_address.setText(myapp.getAddresses().get(i).getAddress());
                    add_new_phone.setText(myapp.getAddresses().get(i).getPhone());
                    break;
                }
            }
            delete_address.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.delete_address:
                deleteOrder();
                if (state == 2) {
                    Toast.makeText(this, "删除数据成功", Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    Toast.makeText(this, "删除数据失败", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.save_addre:
                if (judge == 1) {
                    alterAddress();
                    if (state == 2) {
                        Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }else {
                        Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
                        judge = 1;
                    }
                }else {
                    addAddress();
                    if (state == 2) {
                        Toast.makeText(this, "增加成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }else {
                        Toast.makeText(this, "增加失败", Toast.LENGTH_SHORT).show();
                    }
                }
                state = 0;
                break;
            case R.id.back_from_add_address :
                finish();
                break;
            case R.id.back_from_modify_address :
                finish();
                break;
        }
    }
    //修改地址
    public void alterAddress() {
        Thread t = new Thread(new alterThread());
        t.start();
        try {
            t.join();
        }catch (InterruptedException ie) {
            ie.printStackTrace();
        }
    }
    class alterThread extends Thread {
        @Override
        public void run() {
            Address ad = new Address();

            ad.setAddressId(aid);
            ad.setId(myapp.getUser().getId());
            ad.setName(add_new_name.getText().toString());
            ad.setAddress(add_new_address.getText().toString());
            ad.setPhone(add_new_phone.getText().toString());
            System.out.println(ad.getAddressId() + " " + ad.getId() + " " + ad.getAddress() + " " + ad.getName() + " " + ad.getPhone());
            state = handleData.alterAddre(ad);
        }
    }
    //添加地址
    public void addAddress() {
        Thread t = new Thread(new addThread());
        t.start();
        try {
            t.join();
        }catch (InterruptedException ie) {
            ie.printStackTrace();
        }
    }

    class addThread extends Thread {
        @Override
        public void run() {
            Address ad = new Address();
            ad.setId(myapp.getUser().getId());
            ad.setName(add_new_name.getText().toString());
            ad.setAddress(add_new_address.getText().toString());
            ad.setPhone(add_new_phone.getText().toString());

            System.out.println("order"+aid+"\n"+"id"+myapp.getUser().getId());
            state = handleData.addAddre(ad);
        }
    }

    void deleteOrder() {
        Thread t = new Thread(new deleteThread());
        t.start();
        try {
            t.join();
        }catch (InterruptedException ie) {
            ie.printStackTrace();
        }
    }

    class deleteThread extends Thread {
        @Override
        public void run() {
            state = handleData.dropAddre(aid, myapp.getUser().getId());
        }
    }
}
